import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-03-04
 * Time: 12:57
 */
public class MyStack {
    public int[] elem;
    public int usedSize;
    //初始化
    public MyStack(){
        this.elem = new int[10];
    }
    //压栈
    public void push(int val) {
        //在压栈之前应该检查是否还有空间
        if (isFull()) {
            //2倍扩容
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;
    }
    //判断是否空间已满
    public boolean isFull(){
         return usedSize == elem.length;
    }
    public int pop(){
        if (isEmpty()){
            throw new EmptyException("栈是空的");
        }
        usedSize--;
        return elem[usedSize];//也可以写成return elem[--usedSize]
    }
    public boolean isEmpty(){
        return usedSize == 0;
    }
    //返回栈顶元素
    public int peek(){
        if (isEmpty()){
            throw new EmptyException("栈是空的");
        }
        return elem[usedSize - 1];
    }
}